package com.cy.shop.service.impl;

import com.cy.shop.VO.CartVO;
import com.cy.shop.entity.Address;
import com.cy.shop.entity.Order;
import com.cy.shop.entity.OrderItem;
import com.cy.shop.mapper.OrderMapper;
import com.cy.shop.service.IAddressService;
import com.cy.shop.service.ICartService;
import com.cy.shop.service.IOrderService;
import com.cy.shop.service.ex.InsertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IAddressService addressService;
    @Autowired
    private ICartService cartService;

    @Override
    public Order create(Integer aid, Integer uid, String username, Integer[] cids) {
        /*即将要下单的列表*/
        List<CartVO> list = cartService.getVOByCid(uid,cids);
        //计算产品的总价
        Long totalPrice = 0L;
        for (CartVO c: list){
            totalPrice += c.getRealPrice() * c.getNum();
        }

        Address address = addressService.getByAid(aid,uid);
        Order order =new Order();
        order.setUid(uid);
        /*收货地址数据*/
        order.setRecvName(address.getName());
        order.setRecvPhone(address.getPhone());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityCode());
        order.setRecvArea(address.getAreaName());
        order.setRecvAddress(address.getAddress());
        /*支付、总价、时间*/
        order.setStatus(0);
        order.setTotalPrice(totalPrice);
        order.setOrderTime(new Date());
        /*四个日志*/
        order.setCreatedUser(username);
        order.setCreatedTime(new Date());
        order.setModifiedUser(username);
        order.setModifiedTime(new Date());
        //查询数据
        Integer rows = orderMapper.insertOrder(order);
        if (rows !=1){
            throw new InsertException("插入数据时异常");
        }

        /*创建一个订单项详细数据*/
        for (CartVO c: list){
            /*创建一个订单项数据对象*/
            OrderItem orderItem = new OrderItem();
            orderItem.setOid(order.getOid());
            orderItem.setPid(c.getPid());
            orderItem.setTitle(c.getTitle());
            orderItem.setImage(c.getImage());
            orderItem.setPrice(c.getPrice());
            orderItem.setNum(c.getNum());
            /*四个日志*/
            orderItem.setCreatedUser(username);
            orderItem.setCreatedTime(new Date());
            orderItem.setModifiedUser(username);
            orderItem.setModifiedTime(new Date());
            /*插入数据操作*/
            rows = orderMapper.insertOrderItem(orderItem);
            if (rows !=1){
                throw new InsertException("插入数据时产生异常");
            }
        }
        return order;
    }
}
